package tool;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * Created by Shaarawy11 on 2017/3/22.
 */
public class ESClient {

    public static TransportClient client;

    public ESClient() {
        client = new PreBuiltTransportClient(Settings.EMPTY);
        try {
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.0.107"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public static String insert(String title, String content) throws IOException {
        IndexResponse response = client.prepareIndex("suce", "suce_page", UUID.randomUUID().toString())
                .setSource(jsonBuilder()
                        .startObject()
                        .field("title", title)
                        .field("content", content)
                        .endObject()
                )
                .get();
        String result = "ID: " + response.getId();
        System.out.println(result);
        System.out.println(response.toString());

        return result;
    }

    /*
    * 通过title查询记录
    * */
    public static SearchHit[] queryByTitle(String title){
        SearchResponse response = client.prepareSearch("suce")
                .setTypes("suce_page")
                .setQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery("title", title)))
                .setFrom(0).setSize(10).setExplain(true)
                .setSearchType(SearchType.QUERY_AND_FETCH).execute().actionGet();
        SearchHit[] results = response.getHits().getHits();
        return results;
    }

    /*
    * update数据（可选）
    * */

    public void queryAll(){
        SearchResponse response = client.prepareSearch()
                .setFrom(0).setSize(1000).setExplain(true)
                .get();
        SearchHit[] results = response.getHits().getHits();
        int i = 0;
        for (SearchHit hit : results) {
            System.out.println(i);
            Map<String,Object> result = hit.getSource();
            System.out.println("title: "+result.get("title"));
            System.out.println("keywords: "+result.get("keywords"));
            i++;
        }
    }

    public List queryByTerm(String keywords){
        SearchResponse response = client.prepareSearch("suce")
                .setTypes("suce_page")
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.queryStringQuery(keywords))                 // Query
                .setFrom(0).setSize(10).setExplain(true)
                .get();
        SearchHit[] results = response.getHits().getHits();
        ArrayList<Map> res = new ArrayList<>();
        int i = 0;
        for (SearchHit hit : results) {
            System.out.println(i);
            Map<String,Object> result = hit.getSource();
            result.put("score",hit.getScore());
            res.add(result);
            System.out.println("title: "+result.get("title"));
            System.out.println("content: "+result.get("content"));
            System.out.println("score: "+result.get("score"));
            i++;
        }
        return res;
    }


    public void close() {
        client.close();
    }

    public static void main(String args[]){
        ESClient es = new ESClient();
        try {
//           1、通过爬虫拿到最后一页的数值
//           2、做循环抓取每一页的数据
            Document document = Jsoup.connect("http://news.scuec.edu.cn/?cat=12&paged=" + 2).get();
            Elements elements = document.getElementsByClass("n-post");
            for (int i = 0; i < elements.size(); i++){
                System.out.println("--------" + i + "-------------");
                String oneNewsUrl = elements.get(i).select("a").attr("href");
                Document document1 = Jsoup.connect(oneNewsUrl).get();
                Elements element = document1.getElementsByClass("left-content");
                String title = element.select("h2").text();
                System.out.println(title);
                String content = document1.getElementsByClass("single-content").text();
                System.out.println(content);
//               3、判断数据库中是否有该条记录
//               4、若有则跳过，可用continue;跳过该条信息（建议更新数据库）
//               5、若没有，则保存记录
                System.out.println("1111111111111111111111");
                es.insert(title,content);
                System.out.println("2222222222222222222222222");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        try {
//            es.insert("宇恒副校长一时代风帆行来我院督查党建工作","3月10日上午，校党委常委段超副校长、党委宣传部常务副部长冉春桃一行来我院就学院分党委2016年党建工作开展情况进行监督检查，学院分党委书记朱合理、副院长孙光焰、副书记张文军等分党委委员参加此次述职考核会。 朱合理从学院分党委党建工作的主体责任落实角度就学院分党委的组织建设、“两学一做”开展情况、活动开展情况、党费收缴工作、以及党员发展教育情况等方面进行了工作述职。会上，督查组还对学院党建工作相关材料进行了查阅。冉春桃副部长针对分党委的述职报告和党建工作材料查阅情况进行了点评，并提出针对性的建议。校党委常委段超副校长在最后讲话中对学院的党建工作开展情况表示了充分肯定，并对学院分党委今年的党建工作开展提出了殷切希望和要求，希望学院分党委的党建工作能够在已有的良好基础上做出值得推广借鉴的经验。");
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        es.queryByTerm("宇恒");
//        SearchHit[] results = es.queryByTitle("宇恒副校长一行来我院督查党建工作");
//        if (results.length == 0){
//            System.out.println("保存记录！");
//        }else if (results.length != 0){
//            System.out.println("更新记录！");
//        }
//        int i = 0;
//        for (SearchHit hit : results) {
//            System.out.println(i);
//            Map<String,Object> result = hit.getSource();
//            System.out.println("title: "+result.get("title"));
//            System.out.println("content: "+result.get("content"));
//            i++;
//        }
    }

}
